//
//  HomeFirstTableViewCell.m
//  B2C
//
//  Created by xiaowu on 2017/8/11.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "HomeFirstTableViewCell.h"
#import "FirstImageViewCell.h"
#import "FirstTwoImageViewCell.h"
#import "FirstRecommendCell.h"
#import "CommodityCell.h"

static NSInteger BaseTag = 134134123;

@interface HomeFirstTableViewCell () <UITableViewDataSource, UITableViewDelegate, BHInfiniteScrollViewDelegate>

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) BHInfiniteScrollView *bannerHeadView;

@property (nonatomic, strong) HomeRecommendModel *model;

@property (nonatomic, strong) FirstImageViewCell *firstImageCell;
@property (nonatomic, strong) FirstImageViewCell *secondImageCell;
@property (nonatomic, strong) FirstImageViewCell *thirdImageCell;

@property (nonatomic, strong) FirstTwoImageViewCell *firstTwoImageCell;
@property (nonatomic, strong) FirstRecommendCell *secondRecommendCell;

@property (nonatomic, strong) NSMutableArray *goodsArr;
@property (nonatomic, strong) UIView *footerView;
@end

@implementation HomeFirstTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configureUI];
}

- (void)configureUI {
    self.tableView.backgroundColor = [UIColor backgroundColor];
    self.backgroundColor = [UIColor backgroundColor];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    _goodsArr = [NSMutableArray array];
    
    _firstImageCell = [[[NSBundle mainBundle] loadNibNamed:@"FirstImageViewCell" owner:self options:nil] firstObject];
    _secondImageCell = [[[NSBundle mainBundle] loadNibNamed:@"FirstImageViewCell" owner:self options:nil] firstObject];
    _thirdImageCell = [[[NSBundle mainBundle] loadNibNamed:@"FirstImageViewCell" owner:self options:nil] firstObject];
    
    _firstTwoImageCell = [[[NSBundle mainBundle] loadNibNamed:@"FirstTwoImageViewCell" owner:self options:nil] firstObject];
    _secondRecommendCell = [[[NSBundle mainBundle] loadNibNamed:@"FirstRecommendCell" owner:self options:nil] firstObject];

    [self.tableView registerNib:[UINib nibWithNibName:@"FirstImageViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"imageViewCell"];
    [self.tableView registerClass:[CommodityCell class] forCellReuseIdentifier:@"commodityCell"];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (weakSelf.headerRefresh) {
            weakSelf.headerRefresh();
        }
    }];
    // 初始化tableViewHeader
    [self setHeadBanner];
    self.tableView.tableFooterView = self.footerView;
}

#pragma mark - Method
- (void)setShowData:(HomeRecommendModel *)model {
    [self.tableView.mj_header endRefreshing];
    self.model = model;
    [self setHeadBanner];
    [self.tableView reloadData];
}

- (void)setOthersGoodsData:(NSArray *)arr {
    [self.tableView.mj_header endRefreshing];
    self.goodsArr = [NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
}

- (void)setHeadBanner {
    for (UIView *view in self.headView.subviews) {
        if ([view isKindOfClass:[BHInfiniteScrollView class]]) {
            [view removeFromSuperview];
        }
    }
    
    _bannerHeadView = [BHInfiniteScrollView infiniteScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth*0.5) Delegate:self ImagesArray:[self.model getBannerImgs] PlageHolderImage:PlaceholderImageBanner InfiniteLoop:YES];
    _bannerHeadView.dotSize = 6;
    _bannerHeadView.dotSpacing = 5;
    
    _bannerHeadView.pageControlAlignmentOffset = CGSizeMake(0, 10);
    
    _bannerHeadView.dotColor = [UIColor yellowLineColor];
    _bannerHeadView.selectedDotColor = [UIColor whiteColor];
    
    [self.headView addSubview:self.bannerHeadView];
    self.headView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth * 0.5 + 10);
    self.tableView.tableHeaderView = self.headView;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.goodsArr.count + 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.model.goodshome.count == 0) {
            return 0;
        } else if (self.model.goodshome.count == 1) {
            return 1;
        } else {
            return 2;
        }
    } else if (section == 1) {
        if (self.model.goodsnew.count == 0) {
            return 0;
        } else if (self.model.goodsnew.count == 1) {
            return 1;
        } else {
            return 2;
        }
    } else {
        HomeAllClassGoodsModel *goodsModel = [self.goodsArr safeObjectAtIndex:(section - 2)];
        return [CommodityCell getRowNumberWithItemArr:goodsModel.goods] + 1;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0) {
        NSArray *dataArr = self.model.goodshome;
        // 显示或者隐藏多余imageview
        self.firstImageCell.firstImageView.hidden = (self.model.goodshome.count == 0);
        self.firstTwoImageCell.firstImageView.hidden = NO;
        self.firstTwoImageCell.secondImageView.hidden = (self.model.goodshome.count == 2);
        
        if (indexPath.row == 0) {
            CommodityModel *model = [dataArr safeObjectAtIndex:0];
//            [self.firstImageCell.firstImageView sd_setImageWithURL:[NSURL URLWithString:model.ghomeimg] placeholderImage:PlaceholderImageBanner];
            
            [self.firstImageCell.firstImageView animationWithImageURL:model.ghomeimg placeholderImage:PlaceholderImageBanner];
            
            self.firstImageCell.block = ^{
                if (weakSelf.block) {
                    weakSelf.block(-1, model);
                }
            };
            return self.firstImageCell;
        } else {
            CommodityModel *firstModel = [dataArr safeObjectAtIndex:1];
            CommodityModel *secondModel = [dataArr safeObjectAtIndex:2];
//            [self.firstTwoImageCell.firstImageView sd_setImageWithURL:[NSURL URLWithString:firstModel.ghomeimg] placeholderImage:PlaceholderImagePictures];
//            [self.firstTwoImageCell.secondImageView sd_setImageWithURL:[NSURL URLWithString:secondModel.ghomeimg] placeholderImage:PlaceholderImagePictures];
            
            [self.firstTwoImageCell.firstImageView animationWithImageURL:firstModel.ghomeimg placeholderImage:PlaceholderImagePictures];
            [self.firstTwoImageCell.secondImageView animationWithImageURL:secondModel.ghomeimg placeholderImage:PlaceholderImagePictures];
            
            self.firstTwoImageCell.block = ^(NSInteger index) {
                
                if (weakSelf.block) {
                    if (index == 0) {
                        weakSelf.block(-1, firstModel);
                    } else {
                        weakSelf.block(-1, secondModel);
                    }
                }
            };
            return self.firstTwoImageCell;
        }
    } else if (indexPath.section == 1) {
        NSArray *dataArr = self.model.goodsnew;
        // 显示或者隐藏多余imageview
        self.secondImageCell.firstImageView.hidden = (self.model.goodsnew.count == 0);
        self.secondRecommendCell.firstImageView.hidden = NO;
        self.secondRecommendCell.secondImageView.hidden = (self.model.goodsnew.count == 2);
        self.secondRecommendCell.thirdImageView.hidden = (self.model.goodsnew.count == 2 || self.model.goodsnew.count == 3);
        
        if (indexPath.row == 0) {
            CommodityModel *model = [dataArr safeObjectAtIndex:0];
//            [self.secondImageCell.firstImageView sd_setImageWithURL:[NSURL URLWithString:model.ghomeimg] placeholderImage:PlaceholderImageBanner];
            [self.secondImageCell.firstImageView animationWithImageURL:model.ghomeimg placeholderImage:PlaceholderImageBanner];
            self.secondImageCell.block = ^{
                if (weakSelf.block) {
                    weakSelf.block(-1, model);
                }
            };
            return self.secondImageCell;
        } else {
            CommodityModel *firstModel = [dataArr safeObjectAtIndex:1];
            CommodityModel *secondModel = [dataArr safeObjectAtIndex:2];
            CommodityModel *thirdModel = [dataArr safeObjectAtIndex:3];
//            [self.secondRecommendCell.firstImageView sd_setImageWithURL:[NSURL URLWithString:firstModel.ghomeimg] placeholderImage:PlaceholderImagePictures];
//            [self.secondRecommendCell.secondImageView sd_setImageWithURL:[NSURL URLWithString:secondModel.ghomeimg] placeholderImage:PlaceholderImagePictures];
//            [self.secondRecommendCell.thirdImageView sd_setImageWithURL:[NSURL URLWithString:thirdModel.ghomeimg] placeholderImage:PlaceholderImagePictures];
            
            [self.secondRecommendCell.firstImageView animationWithImageURL:firstModel.ghomeimg placeholderImage:PlaceholderImagePictures];
            [self.secondRecommendCell.secondImageView animationWithImageURL:secondModel.ghomeimg placeholderImage:PlaceholderImagePictures];
            [self.secondRecommendCell.thirdImageView animationWithImageURL:thirdModel.ghomeimg placeholderImage:PlaceholderImagePictures];
            
            
            
            
            
            self.secondRecommendCell.block = ^(NSInteger index) {
                if (weakSelf.block) {
                    if (index == 0) {
                        weakSelf.block(-1, firstModel);
                    } else if (index == 1) {
                        weakSelf.block(-1, secondModel);
                    } else {
                        weakSelf.block(-1, thirdModel);
                    }
                }
            };
            return self.secondRecommendCell;
        }
        
        
    } else {
        HomeAllClassGoodsModel *goodsModel = [self.goodsArr safeObjectAtIndex:(indexPath.section - 2)];

        if (indexPath.row == 0) {
            FirstImageViewCell *imageViewCell = [tableView dequeueReusableCellWithIdentifier:@"imageViewCell" forIndexPath:indexPath];
//            [imageViewCell.firstImageView sd_setImageWithURL:[NSURL URLWithString:goodsModel.gcimg] placeholderImage:PlaceholderImageBanner];
            [imageViewCell.firstImageView animationWithImageURL:goodsModel.gcimg placeholderImage:PlaceholderImageBanner];
            imageViewCell.block = ^{
                if (weakSelf.advBlock) {
                    weakSelf.advBlock(indexPath.section - 2);
                }
            };
            return imageViewCell;
        } else {
            CommodityCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"commodityCell" forIndexPath:indexPath];
            NSArray *arr = [CommodityCell getCellItemArr:goodsModel.goods withRow:indexPath.row - 1];
            goodsCell.backgroundColor = [UIColor backgroundColor];
            [goodsCell setValueForCell:arr];
            goodsCell.block = ^(NSInteger index) {
                CommodityModel *model = [arr safeObjectAtIndex:index];
                if (weakSelf.block) {
                    weakSelf.block(-1, model);
                }
            };
            return goodsCell;
        }
        
    }
    return [[UITableViewCell alloc] init];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (self.model.goodshome.count == 0) {
            return 0;
        } else if (self.model.goodshome.count == 1) {
            if (indexPath.row == 0) {
                return [FirstImageViewCell getHeight];
            } else {
                return 0;
            }
        } else {
            if (indexPath.row == 0) {
                return [FirstImageViewCell getHeight];
            } else {
                return [FirstTwoImageViewCell getHeight];
            }
        }
    } else if (indexPath.section == 1) {
        if (self.model.goodsnew.count == 0) {
            return 0;
        } else if (self.model.goodsnew.count == 1) {
            return [FirstImageViewCell getHeight];
        } else {
            if (indexPath.row == 0) {
                return [FirstImageViewCell getHeight];
            } else {
                return [FirstRecommendCell getHeight];
            }
        }
    } else {
        if (indexPath.row == 0) {
            return [FirstImageViewCell getHeight];
        } else {
            return [CommodityCell getHeight];
        }
    }
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        if (self.model.goodshome.count == 0) {
            return [UIView new];
        }
    } else if (section == 1) {
        if (self.model.goodsnew.count == 0) {
            return [UIView new];
        }
    }
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textColor = [UIColor blackTextColor];
    label.frame = CGRectMake(15, 0, 200, 56);
    if (section == 0) {
        label.text = @"热门推荐";
    } else if (section == 1) {
        label.text = @"新品上市";
    } else {
        HomeAllClassGoodsModel *goodsModel = [self.goodsArr safeObjectAtIndex:(section - 2)];
        label.text = goodsModel.gcname;
    }
    
    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        if (self.model.goodshome.count == 0) {
            return 0.1;
        }
    } else if (section == 1) {
        if (self.model.goodsnew.count == 0) {
            return 0.1;
        }
    }
    
    return 56;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        if (self.model.goodshome.count < 3) {
            return [UIView new];
        }
    } else if (section == 1) {
        if (self.model.goodsnew.count < 4) {
            return [UIView new];
        }
    } else {
        return [UIView new];
    }
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];

    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreButton.frame = CGRectMake(0, 0, tableView.width, 50);
    moreButton.titleLabel.font = TextNormalFont14;
    [moreButton setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
    [moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
    [moreButton setBackgroundColor:[UIColor whiteColor]];
    
    [moreButton setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal];
    
    [moreButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -moreButton.imageView.size.width-3, 0, moreButton.imageView.size.width+3)];
    [moreButton setImageEdgeInsets:UIEdgeInsetsMake(0, moreButton.titleLabel.size.width+3, 0, -moreButton.titleLabel.size.width-3)];
    
    moreButton.tag = section + BaseTag;
    
    [view addSubview:moreButton];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        if (self.model.goodshome.count == 0) {
            return 0.1;
        } else if (self.model.goodshome.count < 3) {
            return 6;
        }
    } else if (section == 1) {
        if (self.model.goodsnew.count == 0) {
            return 0.1;
        } else if (self.model.goodsnew.count < 4) {
            return 6;
        }
    } else {
        return 6;
    }
    return 56;
}

// footer点击回调
- (void)clickMore:(UIButton *)sender {
    NSInteger index = sender.tag - BaseTag;
    if (self.block) {
        self.block(index, nil);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > 1 && indexPath.row == 0) {
        if (self.advBlock) {
            self.advBlock(indexPath.section - 2);
        }
    }
}

#pragma mark - BHInfiniteScrollViewDelegate
- (void)infiniteScrollView:(BHInfiniteScrollView *)infiniteScrollView didSelectItemAtIndex:(NSInteger)index {
    if (self.bannerBlock) {
        self.bannerBlock(index);
    }
}

#pragma mark - Getters
- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = [UIColor backgroundColor];
    }
    return _headView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 60, 0.5)];
        
        lineView.backgroundColor = [UIColor grayTextColor];
        lineView.center = _footerView.center;
        
        NSString *footStr = @"再怎么翻也没有了";
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont12};
        CGFloat length = [footStr boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        
        // 文字
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, length + 30, 40)];
        titleLabel.text = footStr;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = TextNormalFont12;
        titleLabel.textColor = [UIColor grayTextColor];
        titleLabel.backgroundColor = [UIColor backgroundColor];
        
        titleLabel.center = lineView.center;
        
        
        [_footerView addSubview:lineView];
        [_footerView addSubview:titleLabel];
    }
    return _footerView;
}

@end
